//
//  AStockStockListController.m
//  Stock
//
//  Created by blacksun on 11-7-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "AStockStockListController.h"
#import "MyStockListController.h"


@implementation AStockStockListController
@synthesize changeRateLabel;

-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    [super requestFinishedWithResult:aResult andUserinfo:aUserinfo];
    if ([key isEqualToString:@"CRA"]||[key isEqualToString:@"CRD"]) {
        self.tableView.tableFooterView = nil;
    }
}

-(void)queryStocks{
    [super queryStocks];
    
    NSString *uri = nil;
    if ([key isEqualToString:@"CPA"]||[key isEqualToString:@"CPD"]) {
        uri = [NSString stringWithFormat:@"jf/market/sort/%@/%d.jf",[key lowercaseString],currentPage*kRecordPerRequest];
    }else{
        uri = [NSString stringWithFormat:@"jf/market/sort/%@.jf",[key lowercaseString]];
    }
    [self.quotationCenterService queryQuotationStocksWithUri:uri];
}
-(void)viewDidLoad{
    [super viewDidLoad];
    if ([key isEqualToString:@"CPD"]) {
        changeRateLabel.text = @"涨幅";
    }else if ([key isEqualToString:@"CPA"]) {
        changeRateLabel.text = @"跌幅";
    }else if ([key isEqualToString:@"CRD"]) {
        changeRateLabel.text = @"5分钟涨幅";
    }else{
        changeRateLabel.text = @"5分钟跌幅";
    }
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AStock";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        UIColor *clearColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, 90.0, 20.0)];
        nameLabel.backgroundColor = clearColor;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = kStockNameTag;
        UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(110.0, 12.0, 90.0, 20.0)];
        todayPriceLabel.backgroundColor = clearColor;
        todayPriceLabel.tag = kStockTodayPriceTag;
        todayPriceLabel.textAlignment = UITextAlignmentCenter;
        
        UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectMake(210.0, 12.0, 90.0, 20.0)];
        rateLabel.backgroundColor = clearColor;
        rateLabel.tag = kStockChangeRateTag;
        rateLabel.textAlignment = UITextAlignmentRight;
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:todayPriceLabel];
        [cell.contentView addSubview:rateLabel];
       
        
        [nameLabel release];
        [todayPriceLabel release];
        [rateLabel release];
       
        
    }
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    QuotationStock *currentStock = [quotationStocks objectAtIndex:index];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
    UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
    
    UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
    
    NSString *rate = currentStock.changedRate;
    if ([key isEqualToString:@"CRD"] || [key isEqualToString:@"CRA"]) {
        rate = currentStock.percent;
    }
    
    todyLabel.textColor = [UIColor whiteColor];
    rateLabel.textColor = [rate hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
    
    nameLabel.text = currentStock.name;
    if (currentStock.closingPrice && currentStock.closingPrice != 0) {
        todyLabel.text = [currentStock.closingPrice stringValue];
        rateLabel.text = rate; 
    }else{
        todyLabel.text = @"--";
        rateLabel.text = @"--";
    }
    return cell;
}


@end